﻿using Newtonsoft.Json;
using SP.Domain.IRepository;
using SP.Domain.Model;
using SP.Service.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace SP.Service
{
    public class RelationService: IRelationService
    {
        private readonly IUserRoleRepository _user;
        private readonly IRepository<RoleTable> _role;
        private readonly IUserRepository _users;
        private readonly IRepository<PermissionsTable> _permiss;

        public RelationService(IUserRoleRepository user, IRepository<RoleTable> role, IRepository<PermissionsTable> permiss, IUserRepository users)
        {
            _user = user;
            _role = role;
            _permiss = permiss;
            _users = users;
        }

        public int AddRelation(int Uid, string Rid)
        {
            return _user.AddRelation(Uid, Rid);
        }
        //删除角色用户关系
        public int DeleteRelation(RoleUserRelation model)
        {
            return _user.DeleteRelation(model);
        }
        //权限树
        public object PermissList()
        {
            var id = 0;
            return Peritem(id);
        }

        private object Peritem(int id)
        {
            var list = _permiss.GetList().Where(s => s.Pid.Equals(id)).ToList();
            List<object> model = new List<object>();
            list.ForEach(s =>
            {
                if (_permiss.GetList().Where(q => q.Pid.Equals(s.Id)).Count() == 0)
                {
                    model.Add(new
                    {
                        id = s.Id,
                        label = s.PerName
                    });
                }
                else
                {
                    model.Add(new
                    {
                        id = s.Id,
                        label = s.PerName,
                        children = Peritem(s.Id)
                    });
                }
            });
            return model;
        }

        public PageModel<UserRoleRelationOUTDTO> RelationList(int pageIndex, int pageSize, string? UserName)
        {
            //var list = from s in _user.GetAll()
            //           select new UserRoleRelationOUTDTO
            //           {
            //               UserId = s.UserId,
            //               UserName = s.UserName,
            //               RoleNames=s.RoleName,
            //               CreateTime = DateTime.Now,
            //               UpdTime = DateTime.Now,
            //               Isdel = s.Isdel,
            //           };
            var list = _user.GetAll();
            List<UserRoleRelationOUTDTO> listdto = JsonConvert.DeserializeObject<List<UserRoleRelationOUTDTO>>(JsonConvert.SerializeObject(list));
            if (!string.IsNullOrEmpty(UserName))
            {
                listdto = listdto.Where(s => s.UserName.Contains(UserName)).ToList();
            }
            PageModel<UserRoleRelationOUTDTO> pagemodel = new PageModel<UserRoleRelationOUTDTO>();
            pagemodel.Total = listdto.Count();
            pagemodel.List = listdto.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagemodel;
        }

        public List<RoleTable> RoleList()
        {
            return _role.GetList();
        }

        public PageModel<UserTable> UserPageList(int pageIndex, int pageSize, string? UserName)
        {
            return _users.USerPageList(pageIndex,pageSize,UserName);
        }
        /// <summary>
        /// 导航菜单
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object NavPermissList()
        {
            var id = 0;
            return NavPeritem(id);
        }

        private object NavPeritem(int id)
        {
            var list = _permiss.GetList().Where(s => s.Pid.Equals(id)).ToList();
            List<object> model = new List<object>();
            list.ForEach(s =>
            {
                if (_permiss.GetList().Where(q => q.Pid.Equals(s.Id)).Count() == 0)
                {
                    model.Add(new
                    {
                        Id = s.Id,
                        Pid=s.Pid,
                        PerName = s.PerName,
                        IconName=s.IconName,
                        CreateTime=s.CreateTime,
                        PerUrl=s.PerUrl,
                        SortNo=s.SortNo,
                    });
                }
                else
                {
                    model.Add(new
                    {
                        Id = s.Id,
                        Pid = s.Pid,
                        PerName = s.PerName,
                        IconName = s.IconName,
                        CreateTime = s.CreateTime,
                        PerUrl = s.PerUrl,
                        SortNo = s.SortNo,
                        children = NavPeritem(s.Id)
                    });
                }
            });
            return model;
        }
    }
}
